<?php

namespace app\common\model;

use think\Model;

class GoodsCategoryRelation extends Model
{
    public static function me(){
        return new self();
    }
    /**
     * 批量加入商品分类关系
     * @param $goods_id
     * @param $categories
     */
    public function addListGoodsCategoryRelation($goods_id,$categories)
    {
        foreach ($categories as $item) {
            $goods_category_relation = new self();
            $goods_category_relation->goods_id = $goods_id;
            $goods_category_relation->category_id = $item;
            $goods_category_relation->created_time = date("Y-m-d H:i",time());
            $goods_category_relation->save();
        }
    }

    /**
     * 批量删除某个商品的分类
     * @param $goods_id
     */
    public function delGoodsCategoryRelation($goods_id)
    {
        $goods_category_relation = new self();
        $data = $goods_category_relation->where('goods_id',$goods_id);
        $data->delete();
    }

    /**
     * 根据商品id获取此商品的分类(以及分类信息)
     * @param $goods_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getListGoodsCategoryRelation($goods_id)
    {
        $data = $this
            ->field("shop_goods_category.*")
            ->join("shop_goods_category","shop_goods_category.id = shop_goods_category_relation.category_id","right")
            ->where(['shop_goods_category_relation.goods_id'=>$goods_id])
            ->order('shop_goods_category.deep asc')
            ->select();
        return $data;
    }

    /**
     * 根据分类id获取此分类下的全部商品
     * @param $category_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getGoodsCategoryRelationByCategoryId($category_id)
    {
        $data = $this
            ->field("hxxh_goods_category_relation.id as relation_id,hxxh_goods.*")
            ->join("hxxh_goods","hxxh_goods.goods_id = hxxh_goods_category_relation.goods_id","left")
            ->where(['hxxh_goods_category_relation.category_id'=>$category_id])
            ->where(['hxxh_goods.goods_type'=>1])
            ->paginate(20,false,['query'=>request()->param()]);;
        return $data;
    }

    /**
     * 根据主键id获取信息
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     */
    public function getGoodsCategoryRelationById($id)
    {
        $goods_category_relation = new self();
        $data = $goods_category_relation->find($id);
        return $data;
    }

    /**
     * 分类下新增商品
     * @param $param
     * @return bool
     */
    public function add_goods_category_relation($param)
    {
        $goods_category_relation = new self();
        $goods_category_relation->goods_id = $param['goods_id'];
        $goods_category_relation->category_id = $param['category_id'];
        if($goods_category_relation->save())
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * 编辑分类下的某条商品
     * @param $id
     * @param $param
     * @return bool
     */
    public function edit_goods_category_relation($id,$param)
    {
        $goods_category_relation_model = new self();
        $goods_category_relation = $goods_category_relation_model->find($id);
        $goods_category_relation->goods_id = $param['goods_id'];
        $goods_category_relation->category_id = $param['category_id'];
        if($goods_category_relation->save())
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * 删除某个分类下的商品关系
     * @param $id
     * @return bool
     */
    public function del_one_goods_category_relation($id)
    {
        $relation = GoodsCategoryRelation::get($id);
        if ($relation->delete()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 通过社区商品分类id获取分类下的商品
     * @param $category_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getGoodsListByCategoryId($category_id)
    {
        $data = $this->where('category_id',$category_id)->select();
        return $data;
    }

    /**
     * 获取某个商品的分类ids
     * @param $goods_id
     * @return array
     */
    public function get_goods_category_ids($goods_id)
    {
        $category_ids = $this->where('goods_id',$goods_id)->column('category_id');
        return $category_ids;
    }

}
